package xjh.him.activity;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;

import com.xjh.him.R;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

import lombok.Data;
import lombok.EqualsAndHashCode;
import xjh.him.adapter.MsgListAdapter;
import xjh.him.bean.MsgBean;
import xjh.him.common.SharedData;
import xjh.him.netty.codec.Packet;
import xjh.him.netty.dto.ChatDto;

@EqualsAndHashCode(callSuper = true)
@Data
public class HomeMsgActivity extends Activity {
    private static int page = 1;
    private ListView msgList;
    private List<MsgBean> beans;
    private MsgListAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home_msg);
    }

    public void setAdapter(View view) {
        msgList = view.findViewById(R.id.msg_list);

        ChatDto.SelectByUserIdReq req = new ChatDto.SelectByUserIdReq();
        req.setPage(page);
        req.setMeId(SharedData.me.getId());
        SharedData.client.sendMsg(Packet.create(ChatDto.SelectByUserIdReq.class, req));
        beans = new ArrayList<>();
        HashSet<Integer> chatterSet = new HashSet<>();

        Object obj = SharedData.future.get();
        if (obj instanceof ChatDto.SelectByUserIdResp) {
            ChatDto.SelectByUserIdResp resp = (ChatDto.SelectByUserIdResp) obj;
            page = resp.getPage();
            List<ChatDto.ChatItem> chatItemList = resp.getChatItemList();
            for (ChatDto.ChatItem item : chatItemList) {
                beans.add(new MsgBean(item.getChat(), item.getChatMsg()));
                chatterSet.add(item.getChat().getChatterId());
            }
        }

        adapter = new MsgListAdapter(beans, R.layout.home_item, chatterSet);
        msgList.setAdapter(adapter);
    }
}
